package com.yike.user.service.impl;

import com.yike.common.exception.BusinessException;
import com.yike.user.entity.PrivacyAgreement;
import com.yike.user.mapper.PrivacyAgreementMapper;
import com.yike.user.param.PrivacyAgreementParam;
import com.yike.user.service.PrivacyAgreementService;
import com.yike.user.param.PrivacyAgreementPageParam;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yike.common.base.BaseServiceImpl;
import com.yike.common.pagination.Paging;
import com.yike.common.pagination.PageInfo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yike.user.vo.PrivacyAgreementVo;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 隐私协议表 服务实现类
 *
 * @author Sakura
 * @since 2024-09-12
 */
@Slf4j
@Service
public class PrivacyAgreementServiceImpl extends BaseServiceImpl<PrivacyAgreementMapper, PrivacyAgreement> implements PrivacyAgreementService {

    @Autowired
    private PrivacyAgreementMapper privacyAgreementMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean savePrivacyAgreement(PrivacyAgreementParam privacyAgreementParam) throws Exception {
        PrivacyAgreement privacyAgreement = new PrivacyAgreement();
        BeanUtils.copyProperties(privacyAgreementParam, privacyAgreement);
        return super.save(privacyAgreement);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updatePrivacyAgreement(PrivacyAgreementParam privacyAgreementParam) throws Exception {
        PrivacyAgreement privacyAgreement = privacyAgreementMapper.selectById(privacyAgreementParam.getId());
        if (privacyAgreement == null) {
            throw new BusinessException("隐私协议信息异常");
        }
        BeanUtils.copyProperties(privacyAgreementParam, privacyAgreement);
        return super.updateById(privacyAgreement);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deletePrivacyAgreement(Long id) throws Exception {
        return super.removeById(id);
    }

    @Override
    public PrivacyAgreementVo getPrivacyAgreementById(Long id) throws Exception {
        PrivacyAgreement privacyAgreement = privacyAgreementMapper.selectById(id);
        if (privacyAgreement == null) {
            throw new BusinessException("隐私协议信息异常");
        }
        PrivacyAgreementVo privacyAgreementVo = new PrivacyAgreementVo();
        BeanUtils.copyProperties(privacyAgreement, privacyAgreementVo);
        return privacyAgreementVo;
    }

    @Override
    public Paging<PrivacyAgreementVo> getPrivacyAgreementPageList(PrivacyAgreementPageParam privacyAgreementPageParam) throws Exception {
        Page<PrivacyAgreement> page = new PageInfo<>(privacyAgreementPageParam);
        IPage<PrivacyAgreementVo> iPage = privacyAgreementMapper.getPrivacyAgreementList(page, privacyAgreementPageParam);
        return new Paging<PrivacyAgreementVo>(iPage);
    }

}
